	processor 6502
	
	INCLUDE C64defines.asm
	
	
; 10 SYS (4096)
	org $800

	;BYTE	$00, $0E, $08, $0A, $00, $9E, $20, $28, $32, $31, $31, $32, $29, $00, $00, $00
	BYTE	$00, $0E, $08, $0A, $00, $9E, $20, $28, $34, $30, $39, $36, $29, $00, $00, $00

	
	org $1000
	
	jmp start
	
SCREENBUFFER0 = SCREENMEM
SCREENBUFFER1 =  $3C00

BUFFER0SPRITESELECTOR = SCREENBUFFER0+$03f8
BUFFER1SPRITESELECTOR = SCREENBUFFER1+$03f8
	
start

	lda #COLOR_BLACK
	sta 53280
	
	 ; Sprite multicolor stuff
    lda #255
	sta VIC+28

	lda #COLOR_DARKGRAY
	sta VIC+37

	lda #COLOR_LIGHTGRAY
	sta VIC+38
		
	; Set raster IRQ
	jsr setrasterirq
	
	; set the charset
	; POKE 53272,(PEEK(53272)AND240)+ 13
	lda 53272
	and #240
	ora #8
	sta 53272

	; set multicolor mode ON
	; POKE 53270,PEEK(53270)OR 16
	lda 53270
	ora #16
	sta 53270

	; Set multicolor colors
	lda #COLOR_MEDIUMGRAY
	sta 53282
	lda #COLOR_DARKGRAY
	sta 53283
			
	; Set tilemap working screen to buffer 0
	lda #<SCREENBUFFER0
	sta dtm_screen_adress
	
	lda #>SCREENBUFFER0
	sta dtm_screen_adress+1
	
	; Set tilemap backbuffer to buffer 1
	lda #<SCREENBUFFER1
	sta dtm_bbuffer_adress
	
	lda #>SCREENBUFFER1
	sta dtm_bbuffer_adress+1

	lda #<citytiles
	sta dtm_tiles
	
	lda #>citytiles
	sta dtm_tiles+1
	
	lda #<citytilemap
	sta dtm_tilemap
	
	lda #>citytilemap
	sta dtm_tilemap+1
	
	lda #72
	sta dtm_tilemap_width
	
	jsr drawtilemap ; Full map
	
	jsr setcolormemory
	
	
mainloop

	jsr updatescroll
	
	;lda 53280
	;clc
	;adc #1
	;sta 53280
	
	jsr updateplayers
	
	jsr updatebullets
	
	jsr showplayers

	jsr wait4vray
	
	jmp mainloop
	
	
;=================================================
		
wait4vray
		lda hudirqflag
		beq wait4vray
		
		lda #0
		sta hudirqflag
		rts
        ; lda $d012      ; load $d012
        ; cmp #$d2       ; is it equal to #$80?
        ; bne wait4vray  ; if not, keep checking
        ; rts
		 



;=================================================

updatescroll

	; Increase the sub-character scroll (0-7)
	lda scrollpixelx
	clc
	adc #1
	sta scrollpixelx
	
	; We reached end of sub-character scroll (pixel scroll)
	cmp #8
	bne us_noshift
	
	; DO shift a character!
	
	; Check which screen are we using
	
	lda doublebuffer
	bne us_invertedswapbuffers
	
us_swapbuffers

	; Set pixel scroll back to 0
	lda #0
	sta scrollpixelx
	
	; Increase char scroll by 1
	lda dtm_scrollx
	clc
	adc #1
	sta dtm_scrollx

	; Copy screenmem
	
	jsr shifttilemapleft
	
	; Set tilemap working screen to buffer 1
	lda #<SCREENBUFFER1
	sta dtm_screen_adress
	
	lda #>SCREENBUFFER1
	sta dtm_screen_adress+1
	
	; Set tilemap backbuffer to buffer 0
	lda #<SCREENBUFFER0
	sta dtm_bbuffer_adress
	
	lda #>SCREENBUFFER0
	sta dtm_bbuffer_adress+1
	
	jsr drawtilemaplastcolumn	; Repaint the last char
						
	; Update doublebuffer variable, to ack that we are using buffer 1
	lda #1
	sta doublebuffer
				
	jmp us_shifted
	
us_invertedswapbuffers

	; Set pixel scroll back to 0
	lda #0
	sta scrollpixelx
	
	; Increase char scroll by 1
	lda dtm_scrollx
	clc
	adc #1
	sta dtm_scrollx

	; Copy screenmem
	
	jsr shifttilemapleft
	
	; Set tilemap working screen to buffer 0
	lda #<SCREENBUFFER0
	sta dtm_screen_adress
	
	lda #>SCREENBUFFER0
	sta dtm_screen_adress+1
	
	; Set tilemap backbuffer to buffer 1
	lda #<SCREENBUFFER1
	sta dtm_bbuffer_adress
	
	lda #>SCREENBUFFER1
	sta dtm_bbuffer_adress+1
	
	jsr drawtilemaplastcolumn	; Repaint the last char
		
	; Update doublebuffer variable, to ack that we are using buffer 0
	lda #0
	sta doublebuffer
				
	jmp us_shifted
		
us_noshift

us_shifted

	lda #7
	sec
	sbc scrollpixelx
	sta sevenminusscrollpixelx
					
	rts
	
;===================================================

LINE1 = 0
LINE2 = 209

irqmode	byte	0

setrasterirq

	sei          ; turn off interrupts
	lda #$7f
	ldx #$01
	sta $dc0d    ; Turn off CIA 1 interrupts
	sta $dd0d    ; Turn off CIA 2 interrupts
	stx $d01a    ; Turn on raster interrupts

	lda #$1b
	ldx #$08
	ldy #$14
	sta $d011    ; Clear high bit of $d012, set text mode
	stx $d016    ; single-colour
	sty $d018    ; screen at $0400, charset at $2000

	lda #<scrollirq    	; low part of address of interrupt handler code
	ldx #>scrollirq    	; high part of address of interrupt handler code
	ldy #LINE1     		; line to trigger interrupt
	sta $0314    		; store in interrupt vector
	stx $0315
	sty $d012

	lda $dc0d    ; ACK CIA 1 interrupts
	lda $dd0d    ; ACK CIA 2 interrupts
	asl $d019    ; ACK VIC interrupts
	cli

	rts                           ; return to caller
	
;========================================================
scrollirq

	; Mode 1: Scrolling tilemap
	lda #COLOR_ORANGE
	sta 53281
	
	; Set 38 columns mode
	lda 53270
	and #%11110111
	sta 53270
	
	; VIC pixel scroll stuff
	lda 53270
	and #%11111000
	ora sevenminusscrollpixelx
	sta 53270
	
	; Check which is the current buffer, and set the screen to it
	lda doublebuffer
	bne sirq_buffer1
	
	; Set Screen Buffer 0
	lda 53272
	and #%00001111
	ora #%00010000
	sta 53272
	jmp sirq_buffer0
		
sirq_buffer1
		
	; Set Screen Buffer 0
	lda 53272
	and #%00001111
	ora #%11110000
	sta 53272
	
sirq_buffer0
	
	; Set IRQ config for mode 2
	
	lda #LINE2
	sta $d012
	
	lda #<hudirq
	sta $0314
	
	lda #>hudirq
	sta $0315
		 
	 asl $d019    ; ACK interrupt (to re-enable it)
	 pla
	 tay
	 pla
	 tax
	 pla
	 rti          ; return from interrupt
	 
hudirq

	; Mode 2: HUD (Without scroll)
	lda #COLOR_BLACK
	sta 53281
	
	; Set 40 columns mode
	lda 53270
	ora #%00001000
	sta 53270
	
	; VIC pixel scroll stuff (no scroll)
	lda 53270
	and #%11111000
	sta 53270
	
	; Visible screen is always buffer 0	in hud mode
	lda 53272
	and #%00001111
	ora #%00010000
	sta 53272
	
	; Set flag for the vsync
	lda #1
	sta hudirqflag
	
	; Set IRQ config for mode scroll
	
	lda #LINE1
	sta $d012
	
	lda #<scrollirq
	sta $0314
	
	lda #>scrollirq
	sta $0315
		 
	 asl $d019    ; ACK interrupt (to re-enable it)
	 pla
	 tay
	 pla
	 tax
	 pla
	 rti          ; return from interrupt

;===========================================================

setcolormemory

	lda #<cityrowcolors
	sta PGZERO_VECTOR_A
	lda #>cityrowcolors
	sta PGZERO_VECTOR_A+1

	lda #<COLORMEM
	sta PGZERO_VECTOR_B
	lda #>COLORMEM
	sta PGZERO_VECTOR_B+1
	
	ldx #0
	
	
scm_loopy
	
	ldy #0
	lda (PGZERO_VECTOR_A),y
	
scm_loopx
		
	sta (PGZERO_VECTOR_B),y
	
	iny
	cpy #40
	bne scm_loopx
	
	clc
	lda PGZERO_VECTOR_A
	adc #1
	sta PGZERO_VECTOR_A
	lda PGZERO_VECTOR_A+1
	adc #0
	sta PGZERO_VECTOR_A+1
	
	clc
	lda PGZERO_VECTOR_B
	adc #40
	sta PGZERO_VECTOR_B
	lda PGZERO_VECTOR_B+1
	adc #0
	sta PGZERO_VECTOR_B+1
	
	inx
	cpx #20
	bne scm_loopy

	rts

	INCLUDE	WingPlayer.asm
	
	INCLUDE	WingBullet.asm
		 
	INCLUDE	MemoryUtils.asm
		 
	INCLUDE Tilemap.asm
	
scrollx			byte	0
scrollpixelx	byte	0
sevenminusscrollpixelx	byte	0
doublebuffer	byte	0
hudirqflag		byte	0
	
	INCLUDE	City.asm
	
	INCLUDE Sprites.asm
	